
layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var layer = layui.layer;
    /**
     * 目录服务管理
     */
    var DbServiceApplyList = {
        tableId: "dbServicelistApplyTable"
    };

    /**
     * 获取当前用户登录id
     */
    function getUser() {
        $.ajax(Feng.ctxPath + '/databaseListKh/getUser', {
            success: function (res) {
                // console.log(res)
                userId = res.data.id
                console.log(userId)
            }
        })
    }

    $(function () {
        getUser();
    })

    /**
     * 初始化表格的列
     */
    DbServiceApplyList.initColumn = function () {
        return [[
            {field: 'id', align: 'center', hide: true, title: 'id'},
            {field: 'rank', title: '序号', align: 'center', templet: '#rank', style: 'background-color:#ffffff'},
            {
                field: 'department',
                sort: false,
                title: '部门',
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {field: 'dbName', sort: false, title: '数据库', align: 'center', style: 'background-color:#ffffff'},
            {
                field: 'userId',
                sort: false,
                title: '用户id',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'username',
                sort: false,
                title: '账号',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'password',
                sort: false,
                title: '密码',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'servicetype',
                sort: false,
                title: '服务类型',
                align: 'center',
                style: 'background-color:#ffffff',
                templet: function (res) {
                    if (res.servicetype == '1') {
                        return '数据库服务'
                    }
                }
            },
            {field: 'system', sort: false, title: '应用系统', align: 'center', style: 'background-color:#ffffff'},
            {field: 'details', sort: false, title: '说明', align: 'center', style: 'background-color:#ffffff'},
            {
                field: 'open', sort: false, title: '是否公开', align: 'center', templet: function (res) {
                    if (res.open == '1') {
                        return '是'
                    } else {
                        return '否'
                    }
                }, style: 'background-color:#ffffff'
            },
            {field: 'standard', sort: false, title: '使用规范', style: 'background-color:#ffffff'},
            {
                field: 'spState',
                sort: false,
                title: '审批状态',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'remarks',
                sort: false,
                title: '备注',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'revision',
                sort: false,
                title: '乐观锁',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'createdBy',
                sort: false,
                title: '创建人',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'createdTime',
                sort: false,
                title: '创建时间',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'updatedBy',
                sort: false,
                title: '更新人',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {
                field: 'updatedTime',
                sort: false,
                title: '更新时间',
                hide: true,
                align: 'center',
                style: 'background-color:#ffffff'
            },
            {align: 'center', toolbar: '#tableBar', title: '', style: 'background-color:#ffffff'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    DbServiceApplyList.search = function () {
        var queryData = {};

        queryData['department'] = $('#department').val();
        queryData['system'] = $('#system').val();
        queryData['servicetype'] = $('#servicetype').val();

        table.reload(DbServiceApplyList.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 点击查看详情
     *
     * @param data 点击按钮时候的行数据
     */
    DbServiceApplyList.openDetailDlg = function (data) {
        // console.log(data)
        // window.location.replace( Feng.ctxPath + '/databaseListKh/details?id=' + data.id)
        window.location.href = Feng.ctxPath + '/databaseListKh/details?id=' + data.id
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbServiceApplyList.tableId,
        url: Feng.ctxPath + '/databaseListKh/listApply',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: 'line',
        even: false,
        cols: DbServiceApplyList.initColumn(),
        done: function () {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DbServiceApplyList.search();
    });


    // 工具条点击事件
    table.on('tool(' + DbServiceApplyList.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'detail') {
            DbServiceApplyList.openDetailDlg(data);
        }
    });
});
